﻿using System;
using Model;
using Service;
using Resources;

public partial class planningAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnValidateWorkOrder_Click(object sender, EventArgs e)
    {
        if (!ValidateData()) return;
        if (drlITStaff.SelectedIndex != 0)
        {
            Planning planning = GetPlanning();
            new PlanningService().SavePlanning(planning);
        }
    }
    private Planning GetPlanning()
    {
        Utility _Uitility = new Utility();
        Planning planning = new Planning();
        planning.EmployeeId = Int32.Parse(drlITStaff.SelectedValue);
        planning.EstimateTimeInMinute = !String.IsNullOrEmpty(txtEstimatedTime.Text)
                                            ? _Uitility.ConvertToInteger(txtEstimatedTime.Text)
                                            : 0;
        
        planning.PlanningDate = _Uitility.ConvertToCustomizedFormat(txtPlanDate.Text);
        

        if (!String.IsNullOrEmpty(txtPlanTime.Text))
            planning.PlanningTime = DateTime.Parse(txtPlanTime.Text);
        else planning.PlanningTime = DateTime.Parse(DateTime.Now.Hour + ":" + DateTime.Now.Minute);
            planning.Priority = Int32.Parse(drlPriority.SelectedValue);
        planning.Remark = txtRemark.Text;
        planning.TaskId =Int32.Parse(txtWorkOrderID.Text);
        return planning;
    }
    private bool ValidateData()
    {
        if(String.IsNullOrEmpty(txtWorkOrderID.Text))
        {
            lblRequiredWOID.Text = Resource.MsgFillInformation;
            return false;
        }
        lblRequiredWOID.Text = string.Empty;

        if(drlITStaff.SelectedIndex <=0)
        {
            lblRequiredITStaff.Text = Resource.ValueRequire;
            return false;
        }
        lblRequiredITStaff.Text = string.Empty;

        if(String.IsNullOrEmpty(txtPlanDate.Text))
        {
            lblRequiredPlanDate.Text = Resource.MsgFillInformation;
            return false;
        }
        lblRequiredPlanDate.Text = string.Empty;

        if (!String.IsNullOrEmpty(txtPlanTime.Text))
        {
            if (!new Utility().verifyTimeFormat(txtPlanTime.Text))
            {
                lblRequiredPlanTime.Text = Resource.MsgNotCorrectTimeFormat;
                return false;
            }
            lblRequiredPlanTime.Text = string.Empty;
        }
        lblRequiredPlanTime.Text = string.Empty;
        return true;
    }
    private void ClearWorkOrderFields()
    {
        txtRemark.Text = string.Empty;
        txtPlanTime.Text = string.Empty;
        txtEstimatedTime.Text = string.Empty;

        drlITStaff.SelectedIndex = -1;
        drlPriority.SelectedIndex = -1;
        txtPlanDate.Text = string.Empty;
        txtWorkOrderID.Text = string.Empty;
        
    }
    protected void btnCancelWorkOrder_Click(object sender, EventArgs e)
    {
        ClearWorkOrderFields();
    }
    protected void imgButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {

    }
}
